Class 1 – 10/22/2002
Perl Scripting

This class is an introduction to Perl.  It will cover the Perl environment, Perl interpreters, general usage/scope, and style/maintainability.

What is Perl?
Perl is an interesting programming language that started as a simple scripting language for generating and printing reports, and has now developed into a full featured programming language.

Where is Perl used?
•Web development – Almost all CGI scripting is done with Perl.  ISAPI plug in.
•System administration scripts (Monitoring, Reporting, Automation of almost anything)
•Network servers (I've written all sorts of these – encrypted syslogd, web server, etc)

This class will focus on Perl for system administration and network tasks, as this is what the AMD team uses it for primarily.  We will not be focusing on Perl in CGI at this time.

What makes Perl stand out?  How can Perl help me be efficient?
Programming languages differ not so much in what they let you do, but in what they make easy.  Perl is both a very simple language and a very rich language.  You don't have to know everything there is to know about Perl to write useful programs.  Perl tries to grow with you – from simple one line scripts to scripts with hundreds of lines and dozens of plug-in modules, Perl is happy to process whatever you give it.  Perl is especially suited for tasks that involve text processing, reading and writing of files, anything network related, CGI and web page generation, working with databases, and most any kind of console based task.  

What platforms are supported?
•Linux
•*BSD
•Windows NT, 2000, XP, 98, ME, etc.
•Mac
•Almost everything else (even VMS)

Where do you get Perl?
Most Linux/Unix OS's come with it preinstalled, but the latest version can always be downloaded from CPAN.

•For Windows:  http://www.activestate.com/
•For Unix: http://www.perl.com/CPAN/src/stable.tar.gz

What Internet resources are available?
Websites
Main Perl Site		http://www.perl.com/
Perl Mongers		http://www.perl.org/
Perl Monks		http://www.perlmonks.org/
Lean Perl		http://learn.perl.org/
The Perl Documentation Site	http://perldoc.com/
The Perl Intro		http://www.perldoc.com/perl5.8.0/pod/perlintro.html
Picking up Perl Online Book	http://www.ebb.org/PickingUpPerl/pickingUpPerl_toc.html
Boggle your brain		http://www.google.com/

Class 1 – 10/22/2002
Getting Started

Basic programming thought processes and guidelines
•Comments
Source code without comments is like a building without a blueprint.  It's difficult to maintain, and read.  You don't need to get carried away with comments, but the logical flow of your program should be obvious after reading the comments.  Functions (sub-routines) need to have a summary at the top that explains what the function does, what input parameters are required, and what it's output is.
•Variable names
Variables should always be named with sensible names.  Unclear variable names lead to confusion.
•Reusable code
Functions (sub-routines) should always be written with the thought that you will reuse that function in other programs.
•Command line usage
Scripts should not do anything without requiring at least one command line parameter.  No command line parameters should produce a usage summary.
•Error messages and exit codes
Upon successful execution a program should always return an exit status of 0 to the system, and non-zero on error.  All error messages should be descriptive enough to mean something – everyone hates “An error occurred” type errors.
•Versioning
After every group of changes the version of your program should be incremented.
•Changelog
A changelog should be kept of every program you write.
•License
You should always have some type of license at the top of your programs.  It's frustrating when you find code that doesn't have any sort of license in it.

Running Perl programs
•Perl command line options
•-c    Check syntax
•-w    Enable warnings
•-e    Single line script
•-n    Assume while (<>) {} around program

•Explanation of #!/usr/bin/perl -w
•Windows oddities

Basic syntax overview
•Whitespace
Basically all whitespace is irrelevant.
•Comments
Comments start with a pound (#) symbol and go to the end of the line.
•Semi-colons
Every Perl statement must end with a semi-colon.
•Declarations
Perl does not require you to predefine your variables.  You only need to declare sub-routines and report formats.
•Quoting
Quoting can be done with single or double quotes.  All plain text must be quoted.  Variables found in double quotes are evaluated, while variables in single quotes are not evaluated.

Variable types
•Scalar (any single value)
$var = “test message”;
$var = 5;
Reference it as:  print $var;

•Array (a list of scalars)
@var = (“entry 1”, 15, “more text”);
To assign a value to a specific position in an array: $var[10] = 56;
To reference it: print $var[2];

•Hash (a set of key => value pairs)
%var = ( “key1” => “value”, “key2” => 17, 'key3' => 'another value');
You can store data to dynamic hash entries: $var{$someOtherVar} = “hi there”;
To reference it: print $conf{'configurationFileName'};

A Basic Program
print ”Hello world\n”;

Homework
•Install Perl on your computer system.
•Create a simple “Hello World” program and run it.
•Use each of the different types of variables to store the “Hello World” phrase, then and print each of them.